package cn.lovemmd.bms.controller;

import cn.lovemmd.bms.service.AdminService;
import cn.lovemmd.bms.service.CodeService;
import com.alibaba.fastjson2.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/user")
public class AdminController {
    private final AdminService adminService;
    private final CodeService codeService;

    @Autowired
    public AdminController(AdminService adminService, CodeService codeService) {
        this.adminService = adminService;
        this.codeService = codeService;
    }

    @RequestMapping("/login")
    public String login(@RequestParam("userName") String userName,@RequestParam("password") String userPassword) {
        JSONObject jsonObject = new JSONObject();
        int userId = adminService.login(userName, userPassword).getUserId();
        if (userId != 0) {
            jsonObject.put("userId", userId);
            jsonObject.put("userName", userName);
            jsonObject.put("userAuthority", adminService.login(userName, userPassword).getUserAuthority());
        } else {
            jsonObject.put("error", "用户名或密码错误");
        }
        return jsonObject.toString();
    }

    @RequestMapping("/register")
    public String register(@RequestParam("userName") String userName,@RequestParam("password") String userPassword,@RequestParam("authority") String userAuthority) {
        JSONObject jsonObject = new JSONObject();
        int userId = adminService.register(userName, userPassword, userAuthority);
        if (userId != 0) {
            jsonObject.put("userId", userId);
            jsonObject.put("userName", userName);
            jsonObject.put("userAuthority", userAuthority);
        } else {
            jsonObject.put("error", "注册失败");
        }
        return jsonObject.toString();
    }
    @RequestMapping("/signOut")
    public String signOut(@RequestParam("userId") int userId) {
        JSONObject jsonObject = new JSONObject();
        int signOut = adminService.signOut(userId);
        if (signOut != 0) {
            jsonObject.put("msg", "注销成功！");
        } else {
            jsonObject.put("msg", "注销失败");
        }
        return jsonObject.toString();
    }
    @RequestMapping("/updatePassword")
    public String updatePassword(@RequestParam("userId") int userId,@RequestParam("password") String userPassword) {
        JSONObject jsonObject = new JSONObject();
        int updatePassword = adminService.updatePassword(userId, userPassword);
        if (updatePassword != 0) {
            jsonObject.put("msg", "修改成功！");
        } else {
            jsonObject.put("msg", "修改失败");
        }
        return jsonObject.toString();
    }
    @RequestMapping("/addCode")
    public String addCode(@RequestParam("code") String code,@RequestParam("authority") String authority) {
        JSONObject jsonObject = new JSONObject();
        int addCode = codeService.addCode(code,authority);
        if (addCode != 0) {
            jsonObject.put("msg", "添加成功！");
        } else {
            jsonObject.put("msg", "添加失败");
        }
        return jsonObject.toString();
    }
    @RequestMapping("/deleteCode")
    public String deleteCode(@RequestParam("code") String code) {
        JSONObject jsonObject = new JSONObject();
        int deleteCode = codeService.deleteCode(code);
        if (deleteCode != 0) {
            jsonObject.put("msg", "删除成功！");
        } else {
            jsonObject.put("msg", "删除失败");
        }
        return jsonObject.toString();
    }
}
